//
//  QLMainController.swift
//  QdLife
//
//  Created by x on 15/11/6.
//  Copyright © 2015年 Xia. All rights reserved.
//

import UIKit

class QLMainController: UITabBarController, QLBottomBarDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 添加子控制器
        addchildViewControllers()
        // 设置自定义 tabBar
        setBottomBar()
    }
    /**
    设置自定义 tabBar
    */
    private func setBottomBar() {
        view.addSubview(bottomBar)
        tabBar.removeFromSuperview()
    }
    /**
    添加子所有控制器
    */
    private func addchildViewControllers() {
        addChildViewControllerWithStoryboardName("Home", title:"青岛生活圈")
        addChildViewControllerWithStoryboardName("Crux", title:"热点")
        addChildViewControllerWithStoryboardName("Circle", title:"圈子")
        addChildViewControllerWithStoryboardName("Tool", title:"生活工具")
        addChildViewControllerWithStoryboardName("CommunityService", title:"社交服务")
        addChildViewControllerWithStoryboardName("Live", title:"直播")
        addChildViewControllerWithStoryboardName("We", title:"我们")
        addChildViewControllerWithStoryboardName("Surround", title:"周边")
    }
    /**
    从 storyboard 添加一个子控制器
    
    - parameter storyboardName: storyboard
    - parameter title:           控制器标题
    */
    private func addChildViewControllerWithStoryboardName(storyboardName: String, title: String) {
        let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
        let navigationController = storyboard.instantiateInitialViewController() as! QLNavigationController
        navigationController.topViewController!.title = title
        addChildViewController(navigationController)
    }

    // MARK: - QLBottomBarDelegate
    // 选择子控制器的代理方法
    func selectController(index: NSInteger) {
        selectedIndex = index
        
    }
    
    // MARK: - 懒加载
    // 底部自定义 tabBar
    private lazy var bottomBar: QLBottomBar = {
        let bottomBar = NSBundle.mainBundle().loadNibNamed("QLBottomBar", owner: nil, options: nil).last as! QLBottomBar
        bottomBar.delegate = self
        bottomBar.frame = self.tabBar.frame
        return bottomBar
    }()
}
